package org.tools.project

import adbtools.composeapp.generated.resources.Res
import adbtools.composeapp.generated.resources.*
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.PlainTooltip
import androidx.compose.material3.TooltipBox
import androidx.compose.material3.TooltipDefaults.rememberRichTooltipPositionProvider
import androidx.compose.material3.rememberTooltipState
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.ui.tooling.preview.Preview

import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.stringResource
import org.tools.project.vm.MainViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Preview
fun App() {
    Box(
        modifier = Modifier.fillMaxSize().padding(20.dp), contentAlignment = Alignment.TopCenter
    ) {
        Row(modifier = Modifier.fillMaxSize(), verticalAlignment = Alignment.CenterVertically) {
            // 导航栏
            NavigationRail(Modifier.fillMaxHeight()) {
                val pages = Page.entries.toMutableList()
                val viewModel = viewModel { MainViewModel() }
                Column(
                    modifier = Modifier.fillMaxHeight(),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    pages.forEachIndexed { _, page ->
                        TooltipBox(
                            positionProvider = rememberRichTooltipPositionProvider(), tooltip = {
                                PlainTooltip {
                                    Text(stringResource(page.title), style = MaterialTheme.typography.subtitle1)
                                }
                            }, state = rememberTooltipState(), enableUserInput = viewModel.uiPageIndex != page
                        ) {
                            NavigationRailItem(
                                label = { Text(stringResource(page.title)) },
                                icon = { Icon(page.icon, contentDescription = stringResource(page.title)) },
                                selected = viewModel.uiPageIndex == page,
                                onClick = { viewModel.updateUiState(page) },
                                alwaysShowLabel = false,
                            )
                        }
                    }
                }
            }
            var showContent by remember { mutableStateOf(false) }
            Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
                Button(onClick = { showContent = !showContent }) {
                    Text("Click me!")
                }
                AnimatedVisibility(showContent) {
                    val greeting = remember { Greeting().greet() }
                    Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
                        Image(painterResource(Res.drawable.compose_multiplatform), null)
                        Text("Compose: $greeting")
                    }
                }
            }
        }
    }

}

enum class Page(val title: StringResource, val icon: ImageVector) {
    SIGNATURE_INFORMATION(Res.string.签名信息, Icons.Rounded.Description), APK_INFORMATION(
        Res.string.APK信息, Icons.Rounded.Android
    ),
    APK_SIGNATURE(Res.string.APK签名, Icons.Rounded.Pin), SIGNATURE_GENERATION(
        Res.string.签名生成, Icons.Rounded.Key
    ),
    JUNK_CODE(Res.string.垃圾代码, Icons.Rounded.DonutLarge), ICON_FACTORY(
        Res.string.图标工厂, Icons.Rounded.Factory
    ),
    SET_UP(Res.string.设置, Icons.Rounded.Settings)
}